<?php
// +----------------------------------------------------------------------
// | Fanwe 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 云淡风轻(97139915@qq.com)
// +----------------------------------------------------------------------

// 系统配置

class SysConfAction extends CommonAction{
	
	public function index(){
		$list = D($this->getActionName())->where("status=1 and group_id>0 and is_show = 1")->order("id asc")->findAll();
		$conf_list = array(); //用于输出分组格式化后的数组
		
		foreach($list as $k=>$v)
		{
			if($v['name']=='DEFAULT_LANG')
			{
			   $lang_list =D("LangConf")->findAll();
			   $v['val_arr'] = array();
			   foreach($lang_list as $lang_item)
			   {
			   		$v['val_arr'][] = $lang_item['lang_name'];
			   }
			}
			elseif($v['name']=='DEFAULT_USER_GROUP')
			{
			   $group_list =D("UserGroup")->findAll();
			   $v['val_arr'] = array();
			   foreach($group_list as $group_item)
			   {
			   		$v['val_arr'][] = $group_item['id'];
			   }
			}
			else			
			{
				$v['val_arr'] = explode(",",$v['val_arr']);
			}
			if($v['name']!='URL_MODEL')
			$conf_list[L("SYSCONF_GROUP_".$v['group_id'])][$k] = $v;
		}	
		$this->assign("conf_list",$conf_list);
		$this->display();
	}
	
	public function update()
	{
		$upload_list = $this->uploadFile(0,"public");
		if($upload_list)
		{
			foreach($upload_list as $upload_item)
			{
				if($upload_item['key']=="SHOP_LOGO")
				{
					$shop_logo = $upload_item['recpath'].$upload_item['savename'];
				}
				if($upload_item['key']=="WATER_IMAGE")
				{
					$water_image = $upload_item['recpath'].$upload_item['savename'];
				}
				if($upload_item['key']=="NO_PIC")
				{
					$no_pic = $upload_item['recpath'].$upload_item['savename'];
				}
				if($upload_item['key']=="FOOT_LOGO")
				{
					$foot_logo = $upload_item['recpath'].$upload_item['savename'];
				}
				if($upload_item['key']=="TEL_LOGO")
				{
					$tel_logo = $upload_item['recpath'].$upload_item['savename'];
				}
				if($upload_item['key']=="LINK_LOGO")
				{
					$file_name = $this->getRealPath().$upload_item['recpath'].$upload_item['savename'];
					$target_name = $this->getRealPath()."/logo.gif";
					
					if (function_exists("move_uploaded_file"))
					{
						if (move_uploaded_file($file_name,$target_name))
						{
							chmod($target_name,0755);
							$link_logo = "/logo.gif";
							unlink($file_name);
						}
						else if (copy($file_name,$target_name))
						{
							chmod($target_name,0755);
							$link_logo = "/logo.gif";
							unlink($file_name);
						}
					}
					elseif (copy($$file_name,$target_name))
					{
						chmod($target_name,0755);
						$link_logo = "/logo.gif";
						unlink($file_name);
					}
					else
						$link_logo = $upload_item['recpath'].$upload_item['savename'];
				}
			}
		}
		$list = D($this->getActionName())->where("status=1")->findAll();
		foreach($list as $k=>$v)
		{
			$v['val'] = isset($_REQUEST[$v['name']])?$_REQUEST[$v['name']]:$v['val'];
			if($v['name']=="SHOP_LOGO")
			{
				if($shop_logo)
				{
					@unlink($this->getRealPath().$v['val']);
					$v['val'] = $shop_logo;
				}
			}

			if($v['name']=="WATER_IMAGE")
			{
				if($water_image)
				{
					@unlink($this->getRealPath().$v['val']);
					$v['val'] = $water_image;
				}
			}
			if($v['name']=="NO_PIC")
			{
				if($no_pic)
				{
					@unlink($this->getRealPath().$v['val']);
					$v['val'] = $no_pic;
				}
			}
			if($v['name']=="FOOT_LOGO")
			{
				if($foot_logo)
				{
					@unlink($this->getRealPath().$v['val']);
					$v['val'] = $foot_logo;
				}
			}
			if($v['name']=="TEL_LOGO")
			{
				if($tel_logo)
				{
					@unlink($this->getRealPath().$v['val']);
					$v['val'] = $tel_logo;
				}
			}
			if($v['name']=="LINK_LOGO")
			{
				if($link_logo)
				{
					if($link_logo != "/logo.gif" && $v['val'] != "/logo.gif")
						@unlink($this->getRealPath().$v['val']);
						
					$v['val'] = $link_logo;
				}
			}
			if($v['name']=='URL_ROUTE')
			{
				$url_model_cfg_id = D("SysConf")->where("name='URL_MODEL'")->getField("id");
				if($v['val']==0)
				{
					$sql = "update ".C("DB_PREFIX")."sys_conf set val = 0 where id=".$url_model_cfg_id;
				}
				else 
				{
					$sql = "update ".C("DB_PREFIX")."sys_conf set val = 2 where id=".$url_model_cfg_id;
				}
			}
			if($v['name'] == 'PAGE_BOTTOM')
			{
				$v['val'] = stripslashes($v['val']);
			}
			D($this->getActionName())->save($v);
		}
		D("SysConf")->query($sql);

		$a = Dir::delDir($this->getRealPath()."/admin/Runtime/");        
        $f = Dir::delDir($this->getRealPath()."/app/Runtime/");	
        @mkdir($this->getRealPath()."/admin/Runtime/",0777);
        @mkdir($this->getRealPath()."/app/Runtime/",0777);

		$this->success(L('EDIT_SUCCESS'));
	}
	
	public function checkSSL()
	{
		if(extension_loaded("openssl"))
		{
			echo 1;
		}
		else
		{
			echo 0;
		}
	}
}
?>